package com.aiti.lulian.mapper;

import com.aiti.lulian.workflow.TaskHistory;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;


import java.util.List;

public interface TaskHistoryMapper extends BaseMapper<TaskHistory> {

    default List<TaskHistory> findByProcessInstanceId(String processInstanceId) {
        return selectList(new QueryWrapper<TaskHistory>()
                .eq("process_instance_id", processInstanceId));
    }

    default List<TaskHistory> findByProcessInstanceIdOrderByActionTimeAsc(String processInstanceId) {
        return selectList(new QueryWrapper<TaskHistory>()
                .eq("process_instance_id", processInstanceId)
                .orderByAsc("action_time"));
    }

    default List<TaskHistory> findByTaskInstanceId(String taskInstanceId) {
        return selectList(new QueryWrapper<TaskHistory>()
                .eq("task_instance_id", taskInstanceId));
    }

    @Select("SELECT * FROM dj_task_history " +
            "WHERE process_instance_id = #{processInstanceId} AND task_definition_id = #{taskDefinitionId}")
    List<TaskHistory> findByProcessInstanceIdAndTaskDefinitionId(
            @Param("processInstanceId") String processInstanceId,
            @Param("taskDefinitionId") String taskDefinitionId);

    @Select("SELECT * FROM dj_task_history " +
            "WHERE process_instance_id = #{processInstanceId} " +
            "ORDER BY action_time DESC")
    List<TaskHistory> findLatestByProcessInstanceId(@Param("processInstanceId") String processInstanceId);
}